package com.wut.clock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wut.clock.common.constant.CommentConstant;
import com.wut.clock.common.constant.MessageConstant;
import com.wut.clock.common.exception.Asserts;
import com.wut.clock.common.service.RedisService;
import com.wut.clock.common.util.RedisKeyUtil;
import com.wut.clock.common.util.SensitiveFilterUtil;
import com.wut.clock.entity.Article;
import com.wut.clock.entity.Comment;
import com.wut.clock.entity.Message;
import com.wut.clock.event.EventProducer;
import com.wut.clock.mapper.ArticleMapper;
import com.wut.clock.mapper.CommentMapper;
import com.wut.clock.mapper.SysUserMapper;
import com.wut.clock.service.ICommentService;
import com.wut.clock.util.ShiroUtil;
import com.wut.clock.vo.portal.CommentPortalVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ZYH
 * @unit D9Lab
 * @description 接口实现类
 * @date 2022/6/8 11:22
 */
@Service
@RequiredArgsConstructor
public class CommentServiceImpl implements ICommentService {

    private final CommentMapper commentMapper;

    private final SensitiveFilterUtil sensitiveFilterUtil;

    private final SysUserMapper userMapper;

    private final ArticleMapper articleMapper;

    private final EventProducer eventProducer;

    private final RedisTemplate redisTemplate;


    @Transactional
    @Override
    public int addComment(Comment comment) {
        comment.setUserId(ShiroUtil.getUserId());
        Map<String, String> filterMap = sensitiveFilterUtil.filter(comment.getCommentContent());
        if(StringUtils.isNotBlank(filterMap.get("sensitiveText"))) Asserts.fail("输入包含敏感词");
        // 生成一个事件
        Message message = new Message();
        message.setUserId(comment.getReplyUserId());
        message.setFromUserId(ShiroUtil.getUserId());
        message.setTargetId(comment.getTopicId());
        message.setTargetType(comment.getTopicType());
        message.setType(MessageConstant.Type.COMMENT.getCode());
        message.setContent(comment.getCommentContent());
        eventProducer.fireCommentEvent(message);
        String redisKey = RedisKeyUtil.getArticleScore();
        redisTemplate.opsForSet().add(redisKey,comment.getArticleId());
        return commentMapper.insert(comment);
    }

    @Override
    public int deleteComment(Integer commentId) {
        // 删除所有的子评论

        String redisKey = RedisKeyUtil.getArticleScore();
        redisTemplate.opsForSet().add(redisKey,commentMapper.selectById(commentId).getArticleId());
        return commentMapper.deleteById(commentId);
    }

    @Override
    public List<CommentPortalVo> getOwnCommentRecord(String keywords) {
        List<CommentPortalVo> commentPortalVoList = new ArrayList<>();

        // 当前用户的userId
        Integer userId = ShiroUtil.getUserId();

        // 根据当前用户去获取他的评论列表
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        if(StringUtils.isNotBlank(keywords)) wrapper.like("comment_content",keywords);
        List<Comment> commentList = commentMapper.selectList(wrapper);

        // 遍历
        for (Comment comment : commentList) {
            CommentPortalVo commentPortalVo = new CommentPortalVo();
            String nickname = userMapper.selectById(comment.getReplyUserId()).getNickname();
            commentPortalVo.setReplyNickname(nickname);
            BeanUtils.copyProperties(comment,commentPortalVo);
            int topicType = 1;
            if(comment.getTopicType().equals(CommentConstant.TopicType.ARTICLE.getCode())){
                Article article = articleMapper.selectById(comment.getTopicId());
                commentPortalVo.setArticle(article);
            }else{
                while(topicType != 0){
                    comment = commentMapper.selectById(comment.getTopicId());
                    topicType =  comment.getTopicType();
                }
                Article article = articleMapper.selectById(comment.getTopicId());
                commentPortalVo.setArticle(article);
            }
            commentPortalVoList.add(commentPortalVo);
        }
        return commentPortalVoList;
    }
}
